package com.lhy.vaccine_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhy.vaccine_admin.entity.SAdmin;
import com.lhy.vaccine_admin.feign.VaccineCheck;
import com.lhy.vaccine_admin.form.LoginForm;
import com.lhy.vaccine_admin.form.SearchForm;
import com.lhy.vaccine_admin.service.IUserService;
import com.lhy.vaccine_admin.service.impl.SAdminService;
import com.lhy.vaccine_admin.vo.R;
import com.lhy.vaccine_admin.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhy
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/admin/sAdmin")
public class SAdminController {

    @Autowired
    private SAdminService adminService;

    @Autowired
    VaccineCheck vaccineCheck;

    @Autowired
    IUserService userService;

    /**
     *  登录界面
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginForm loginForm){
        R r=adminService.login(loginForm);
        return r;
    }

    /**
     * 远程调用  查询数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/pageList/{currentPage}/{pageSize}")
    public R getVaccineByList(@PathVariable("currentPage") Integer currentPage,
                              @PathVariable("pageSize") Integer pageSize){
        R vaccineList = vaccineCheck.getVaccineList(currentPage, pageSize);
        return vaccineList;
    }

    /**
     * 远程调用  关键字查询
     * @param searchForm
     * @return
     */
    @PostMapping("/search")
    public R getVaccineByLike(@RequestBody SearchForm searchForm){
        R selectByLike = vaccineCheck.getSelectByLike(searchForm);
        return selectByLike;
    }

    /**
     *  账户管理界面
     *  获取管理员列表
     * @return
     */
    @GetMapping("/getAdminList/{currentPage}/{pageSize}")
    public R getAdminList(@PathVariable("currentPage") Integer currentPage,
                          @PathVariable("pageSize") Integer pageSize){
        R list = adminService.getAdminList(currentPage,pageSize);

        return list;
    }

    /**
     * 账户管理界面
     * 添加管理员界面
     * @param sAdmin
     * @return
     */
    @PostMapping("/insertAdmin")
    public R insertNewAdmin(@RequestBody SAdmin sAdmin){
        R r = adminService.insertNewAdmin(sAdmin);
        return r;
    }

    /**
     * 账户管理界面
     * 删除管理员界面
     * @param sAdmin
     * @return
     */
    @PostMapping("/deleteNewAdmin")
    public R deleteNewAdmin(@RequestBody SAdmin sAdmin){
        System.out.println(sAdmin);
        R r = adminService.deleteNewAdmin(sAdmin);
        return r;
    }

    @GetMapping("/searchByKey")
    public R getSearchByKey(String newValue){
        R r = adminService.getSearchByKey(newValue);
        return r;
    }

    @Transactional
    @PostMapping("/putuser")
    public R insertUser(@RequestBody UserVo userVo){
        QueryWrapper<UserVo> wrapper = new QueryWrapper<>();
        wrapper.eq("u_phone",userVo.getPhone());
        UserVo one = userService.getOne(wrapper);
        if (one != null){
            return R.error().put("data","0");
        }
        boolean save = userService.save(userVo);
        if (save){
            return R.ok().put("data", "1");
        }
        return R.error().put("data","2");
    }

    @Transactional
    @PostMapping("/getuser")
    public R getUser(@RequestBody UserVo userVo){
        UserVo vo = userService.getOne(new QueryWrapper<UserVo>().eq("u_phone", userVo.getPhone()));
        if (vo!=null){
            return R.ok().put("data",vo.getId());
        }
        return R.error().put("data","");
    }

    @GetMapping("/getuserbyid")
    public R getUserById(Integer uId){
        UserVo id = userService.getById(uId);
        if (id!=null){
            return R.ok().put("data",id.getName());
        }
        return R.error().put("data","没有数据!");
    }
}

